/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.perlak.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Date utility class to convert protobuf string time representation to Calendar
 * @author mateusz
 */
public class DataUtil {

  /**
   * Formatter. TODO: add timezone
   */
  private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  
  /**
   * Gets Calendar of protobuf date representation.
   * @param dateString protobuf date string representation
   * @return Calendar object of date
   */
  public static Calendar fromProtobuf(final String dateString) {
    Calendar cal = Calendar.getInstance();
    try {
      cal.setTime(formatter.parse(dateString));
    } catch (ParseException ex) {
    }
    return cal;
  }
  /**
   * Gets protobuf date string representation for Calendar date
   * @param calDate Calendar object
   * @return protobuf date string representation
   */
  public static String toProtobuf(final Calendar calDate) {
    return formatter.format(calDate.getTime());
  }
  /**
   * Gets protobuf date string representation for Date.
   * @param date Date object.
   * @return protobuf date string representation
   */
  public static String toProtobuf(final Date date) {
    return formatter.format(date);
  }
}
